package cn.zd.demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ActivityBackActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView;
    private Button btn;
    private EditText editText;
    private Button btn_sub;
    private Button btn_add;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Toast.makeText(this,this.getClass().getName(),Toast.LENGTH_LONG).show();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_back);
        textView = findViewById(R.id.sec_tv);
        btn = findViewById(R.id.btn_finish);
        btn_sub = findViewById(R.id.btn_sub);
        btn_add = findViewById(R.id.btn_add);
        editText = findViewById(R.id.et_count);
        Intent intent = getIntent();
        Log.i("MainActivity2","intent"+ intent);
        Bundle extras = intent.getExtras();
        if(extras != null) {
            int count = extras.getInt("count");
            textView.setText(String.format("据说点击了%s次", count));
            editText.setText(count +"");
        } else{
            textView.setText("没有接收到数据");
        }
        btn.setOnClickListener(this);
        btn_sub.setOnClickListener(this);
        btn_add.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int i = Integer.parseInt(editText.getText().toString());
        Toast.makeText(this, "当前值：" + i, Toast.LENGTH_LONG).show();
        if (view.getId() == btn_sub.getId()) {
            i--;
            editText.setText(""+i);
        } else if (view.getId() == btn_add.getId()) {
            i++;
            editText.setText(""+i);
        } else if (view.getId() == btn.getId()) {
            Toast.makeText(this, "我要返回了！" + editText.getText().toString(), Toast.LENGTH_LONG).show();
            Intent data = new Intent();
            Bundle bo = new Bundle();
            bo.putInt("count",i);
            data.putExtras(bo);
            setResult(RESULT_OK,data);
            finish();
        }

    }
}